import redis
class MyRedis:
    def __init__(self) -> None:
        # 创建 Redis 连接池  
        pool = redis.ConnectionPool(host='124.71.227.70', port=6379, db=0, max_connections=10)  
        # 使用连接池创建 Redis 连接对象  
        self.r = redis.Redis(connection_pool=pool)  
       
        
    #字符串添加
    def str_set(self,key,value):
        return self.r.set(key,value)
    
    #加过期时间
    def str_set_exp(self,key,value,time):
        return self.r.setex(key,time,value)
    
    #字符串获取
    def str_get(self,key):
        value =self.r.get(key) 
        if value:
            value = value.decode()
        return value
    
    def int_jian(self,key):
       return  self.r.decr(key)
    
    #分步式锁
    def setnx_str(self,key,value):
       return self.r.setnx(name=key,value=value)
   
    #删除key
    def key_del(self,key):
        return self.r.delete(key)
    
    #redis list
    #添加
    # def  list_add(self,key,value):
    #     return self.r.lpush(key,value)

    def list_push(self,key,value):
        return self.r.rpush(key,value)

    #获取所有
    def list_all(self,key):
        data = self.r.lrange(key,0,-1)
        return data

    #pop单个
    def list_pop(self,key):
        return self.r.rpop(key)

    # def list_pop1(self,key):
    #     return self.r.lpop(key)
    
    #获取队列的长度
    def list_len(self,key):
        return self.r.llen(key)

    #删除指定元素
    def list_del(self,key,value):
        return self.r.lrem(key,0,value)
    
    #zset添加
    def zset_add(self,key,value,score):
        mapping={value:score}
        return self.r.zadd(key,mapping=mapping)
    
    def zset_del(self,key,value):
        return self.r.zrem(key,value)
    
    def zrange_score(self,key,start,end):
        return self.r.zrangebyscore(key,start,end)
# import time
r = MyRedis()

# r.zset_add('orderlist','1001',int(time.time()))
# time.sleep(1)
# r.zset_add('orderlist','1002',int(time.time()))
# time.sleep(1)
# r.zset_add('orderlist','1003',int(time.time()))

# print(r.zrange_score('orderlist',0,int(time.time())-1000))

# r.str_set('bdapicount',10)
# r.int_jian('bdapicount')
# print(r.str_get("bdapicount"))

# r.list_add('list1','c')
# r.list_add('list1','d')
# r.list_pop('list1')
# print(r.list_all('list1'))
# r.str_set('a','b')
# print(r.str_get('a'))
# print(r.setnx_str('abcd22111222','123aaa'))
# # print(r.str_setnx('a11','456'))
# print(r.str_get('aert3tdgdet32r211'))
# r.str_set('abc','124234')
# print(r.str_get('abc3'))